﻿
<!--#Include File = "CustomTemplate.asp" -->

<%
	Call  PageController.RenderPage()
	Public Function Page_Configure()
		PageController.PageTitle = "Advertising management"
		PageController.AuthorizedRoles =  Array("editor","publisher","admin")
		pageController.RequiresAuthentication  = True
		PageController.PageCSSFiles = "main.css" 		
	End function
%>
<%Public Function Page_RenderForm()%>
	<H2>Advertising management</H2>
	

	
	<%Page.Execute%>	
	<Span Class="Caption">Advertising Edit<HR></Span>
	<a href="javascript:clasp.form.doPostBack('SetVisibility', 'Panel')">Hide/Show Panel!</a>

	<%panel%>
	
	
<%End Function%>

<%  '
	Dim txtName
	Dim txtUrl

	Dim cboPosition
	Dim cboType
	
	
	Dim Panel

	Dim mValidation

	Dim cmdSave	
	Dim recordID
	
	'For upload images
	Dim cmdFile	
	Dim txtFileName
	Dim imgViewer
	
		
	Public Function Page_Init()
		recordID = 0  'Init
		
		
		Set  txtName	= New_ServerTextBoxEX("txtName",100,40)
		Set  txtUrl	= New_ServerTextBoxEX("txtUrl",255,40)	
		
		Set  cboPosition = New_ServerDropDown("cboPosition")	
		Set  cboType = New_ServerDropDown("cboType")		
		
		Set cmdSave = New_ServerLinkButton("cmdSave")		
		
		Set mValidation = New ServerValidationSummary
		Set panel = New_ServerPanel("Panel",870,800)
	
		
		'For upload file
		Set txtFileName				= New_ServerTextBox("txtFileName")
		Set imgViewer			= New_ServerImage("imgViewer")
		Set cmdFile 			= New_ServerFileUploader("cmdFile","")
		cmdFile.TempUploadPath	= Server.MapPath("../Uploads/advs/")	
		
		
	End Function

	Public Function Page_Controls_Init()		
		cmdSave.Text = "Save"
		
		
		panel.Mode = 2
		'panel.OverFlow = "auto" 'scroll?
		panel.PanelTemplate = "RenderPanel"
		
		cboPosition.Items.Append "Main","main",False
		cboPosition.Items.Append "Top Right","topright",True
		cboPosition.Items.Append "Bottom Right","bottomright",False
		
		cboType.Items.Append "Image","image",True
		cboType.Items.Append "Flash","flash",False
		
		'For upload file
		txtFileName.Text = "yourfile"	
		cmdFile.Text = "Upload File"	
		cmdFile.FileFilter = "bmp,jpeg,jpg,gif,swf" 	'optional, set to "" to upload any file
		cmdFile.MaxUploadSize = 1000000				'optional, set to 0 to uplaod any size
	End Function

	Public Function Page_Load()
	
		If Not Page.IsPostBack Then	
			If Request.QueryString("id")<>"" Then
				recordID = Clng(Request.QueryString("id"))					
				 
			End if
			Call LoadRecord()
	
		End If	
		
	End Function
	
	Public Function Page_LoadViewState()
		recordID = Page.ViewState.GetValue("ID") 'Get from viewstate	
		
	End Function
	Public Function Page_PreRender()
		Page.ViewState.Add "ID",recordID 'Add to viewstate :-)
		
		'For upload file
		If imgViewer.ImageSrc ="" Then
			imgViewer.Control.Visible = False
		Else
			imgViewer.Control.Visible = True
		End If	
	
	End Function

	Public Function cmdSave_OnClick()
		Dim sSQL
		Dim rs
		
		
		Call Validate()
		If Not mValidation.IsValid Then
			Exit Function
		End If		
		
		
		'For upload file
		If txtFileName.Text <> "" Then
			cmdFile.FileName = txtFileName.Text
			cmdFile.SaveFile(Server.MapPath("../Uploads/advs/"))
			imgViewer.ImageSrc = "../Uploads/advs/"+txtFileName.Text
			'imgViewer.Width = "300px"
			
		End If
		
		
		sSQL = "SELECT * FROM advertising Where id=" & recordID	
		
		
		Set rs = DBLayer.GetRecordSet(sSQL)		
		
		If rs.RecordCount = 0 Then
			rs.AddNew			
		
		End If		
		
		rs("name").Value	= txtName.Text
		rs("url").Value    = txtUrl.Text
		rs("position").Value    = cboPosition.Value	
	
		rs("status").Value = 1
		rs("isdeleted").Value = 0
		rs("type").Value	= cboType.Value
		rs("filename").Value  = txtFileName.Text
	
		' update advertising table
		DBLayer.UpdateRecordSet rs
	
		
		If mValidation.IsValid Then
			Response.Redirect "Adv.asp"
		End If
		
	End Function
	
	Public Function Validate()
		
		'You could use several approaches to validation. I normaly use this approach and modify the control output as appropriate
	
		'(object,strFriendlyName, intDataType, bolRequired, strDefaultValue)
		mValidation.Validate 	txtName,"Name", VALIDATION_DATATYPE_STRING, True , ""
		mValidation.Validate 	cboPosition,"Position", VALIDATION_DATATYPE_STRING, True , ""
		
		
	End Function

	Public Function LoadRecord()
		Dim sSQL
		Dim rs
		
	
		
		sSQL = "SELECT * FROM advertising Where id=" & recordID
		Set rs = GetRecordSet(sSQL)
		
		
		If rs.RecordCount > 0 Then
			txtName.Text = rs("name").Value
			txtFileName.Text = rs("filename").Value
			txtUrl.Text  = rs("url").Value
			cboPosition.Items.SetSelectedByValue rs("position").Value,True
			cboType.Items.SetSelectedByValue rs("type").Value,True
	
		End If
			
	End Function

	Public Function Panel_SetVisibility(e) 
		Panel.Control.Visible = Not Panel.Control.Visible
	End Function
	
	'for upload file
	Public Function cmdFile_OnUpload()
		imgViewer.ImageSrc = "../Uploads/advs/" + cmdFile.TempFileName
		txtFileName.Text = year(now()) & month(now()) & day(now())& hour(now()) & minute(now())& second(now()) & cmdFile.FileName 
	
	End Function
	

%>


<%Function RenderPanel() %>
<%mValidation%>
	<table border=0 ID="Table1" width="80%">
		<tr><td>Adv name </td><td><%txtName%></td></tr>
		<tr><td>Url</td><td><%txtUrl%></td></tr>
		<tr><td>Position</td><td><%cboPosition%></td></tr>
		<tr><td>Type</td><td><%cboType%></td></tr>
		<tr><td>Filename</td><td><%txtFileName%>&nbsp;<font size="2"><%cmdFile%></font></td></tr>
		<tr><td>&nbsp;</td><td><%imgViewer%></td></tr>
		<tr><td colspan=2 align=left><a href="Adv.asp"><HR>Back</a> | <%cmdSave%></td></tr>
	</table>
	
	
<%End Function%>
